package com.ming.sportdemo.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ming.sportdemo.annotation.NeedFinalRole;
import com.ming.sportdemo.annotation.UserToken;
import com.ming.sportdemo.bean.*;
import com.ming.sportdemo.common.Result;
import com.ming.sportdemo.dao.OrderDao;
import com.ming.sportdemo.service.ClothesService;
import com.ming.sportdemo.service.FieldService;
import com.ming.sportdemo.service.OrderService;
import com.ming.sportdemo.service.UtilService;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


@RestController
public class OrderController {
    OrderDao orderDao;

    @Autowired
    public void setOrderDao(OrderDao orderDao) {
        this.orderDao = orderDao;
    }

    @Autowired
    private OrderService orderService;

    @Autowired
    FieldService fieldService;
    @Autowired
    ClothesService clothesService;
    @Autowired
    UtilService utilService;

    @RequestMapping("/allOrder")
    public String allOrder(QueryInfo queryInfo) {
        int numbers = orderService.getOrderCounts("%" + queryInfo.getQuery() + "%");
        int pageStart = (queryInfo.getPageNum() - 1) * queryInfo.getPageSize();
        List<Order> order = orderService.getAllOrder("%" + queryInfo.getQuery() + "%", pageStart, queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers", numbers);
        res.put("data", order);
        String res_string = JSON.toJSONString(res);
        return res_string;
    }


    @RequestMapping("/updateOrderStates")
    public Result updateOrderStates(HttpServletRequest request, @RequestParam("orderId") Integer orderId, @RequestParam("orderState") Boolean orderState){
        int i = orderService.updateOrderStates(orderId,orderState);
        String res = i>0?"success":"error";
        if(res.equals("success")){
            return Result.succ(res);
        }else {
            return Result.fail(res);
        }

    }

    @RequestMapping("/addOrder")
    public String addOder(@RequestBody OrderClient orderClient){
        System.out.println(orderClient.getUserId()+"."+orderClient.getClothesName()+"."
                +orderClient.getFieldName()+"."+orderClient.getUtilName());
        Order res = new Order();
        res.setUserId(orderClient.getUserId());
        int clothesId= clothesService.getByName(orderClient.getClothesName()).getClothesId();
        int fieldId = fieldService.getByName(orderClient.getFieldName()).getFieldId();
        int utilId = utilService.getByName(orderClient.getUtilName()).getUtilId();
        Clothes clothes = clothesService.getById(clothesId);
        Util util = utilService.getById(utilId);
        boolean flag = false;
        if(clothes.getClothesNumber()>0&&util.getUtilNumber()>0){
            clothesService.sub(clothesId);
            utilService.sub(utilId);
            res.setUtilId(utilId);
            res.setFieldId(fieldId);
            res.setClothesId(clothesId);
            double total = clothesService.getByName(orderClient.getClothesName()).getClothesPrice()+
                    fieldService.getByName(orderClient.getFieldName()).getFieldPrice()+
                    utilService.getByName(orderClient.getUtilName()).getUtilPrice();
            res.setTotal(total);
            res.setOrderState(false);
            Date date = new Date();
            Timestamp sqlDate = new Timestamp(date.getTime());

            //res.setOrderDate(sqlDate);
            System.out.println(res);
            flag = orderService.save(res);
        }else {
            System.out.println("商品不足");
        }

        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        return flag?"success":"error";
    }

    @RequestMapping("/deleteOrder")
    public String deleteOrder(int orderId){
        int i = orderService.deleteOrder(orderId);
        return i>0?"success":"error";
    }

    @RequestMapping("/updateOrder")
    public String updateOrder(@RequestBody Order order){
        System.out.println(order);
        int i = orderService.updateOrder(order);
        System.out.println(i);
        System.out.println(order);

        int fieldId = order.getFieldId();
        Field field = fieldService.getById(fieldId);
        double FieldPrice = field.getFieldPrice();

        int clothesId = order.getClothesId();
        Clothes clothes=clothesService.getById(clothesId);
        double ClothesPrice = clothes.getClothesPrice();

        int utilId = order.getUtilId();
        Util util=utilService.getById(utilId);
        double UtilPrice=util.getUtilPrice();

        double sum=FieldPrice+ClothesPrice+UtilPrice;
        order.setTotal(sum);
        orderService.updateOrder(order);
        return i>0?"success":"error";
    }
}
